<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\News as NewsModel;
/**
 * Sharedailyimg 分享每日一禅图片
 * Class Share
 * @package app\home\controller
 */
class Sharedailyimg extends Base
{
    /**
     * [index  分享每日一禅图片]
     * @author [C.]
     * @return mixed
     */
    public function index()
    {
        $date = explode(',',date('n,d'));
        $day = $date[1];
        $montharray = array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
        $month = $montharray[$date[0] - 1];
        $weekarray=array("日","一","二","三","四","五","六");
        $week = "周".$weekarray[date("w")];

        $param = input('param.');
        $model = new NewsModel;
        $user = $this->user;
        try{
            $daily = $model->findData($param['id'],'id','cover,share_top,share_buttom,from');
            $cover = substr($daily['cover'],strripos($daily['cover'],"/")+1);

            $uploaddir = "img/";//设置文件保存目录 注意包含
            if (!file_exists($uploaddir)) {
                mkdir($uploaddir, 0755, true);
            }
            $uploaddir1 = "nouse/";//设置文件保存目录 注意包含
            if (!file_exists($uploaddir1)) {
                mkdir($uploaddir1, 0755, true);
            }

            //header("content-type:image/jpg");
            $content1 = "南无阿弥陀佛！";
            $content2 = '师兄，想要了解更多佛教内容，';
            $content3 = '扫描左边的二维码，我在';
            $content4 = '福善Online';
            $content6 = '等您！';
            $content7 = '福善 Online';

            $im = imagecreatetruecolor(750, 1270); // 设置画布
            $color = imagecolorallocate($im, 255, 255, 255); //为画布分配颜色
            imagefill($im,0,0,$color); //填充颜色
            $font = ROOT_PATH.'public/static/font/msyh.ttf';               // 设置字体
//            $blacka = imagecolorallocate($im, 51, 51, 51);  // 颜色
//            $blacka1 = imagecolorallocate($im, 255, 152, 0);  // 颜色

//            imagettftext($im, 16, 0, 214, 690, $blacka, $font, $content1);      // 写入积分
//            imagettftext($im, 16, 0, 214, 725, $blacka, $font, $content2);      // 写入积分
//            imagettftext($im, 16, 0, 214, 763, $blacka, $font, $content3);      // 写入积分
//            imagettftext($im, 16, 0, 445, 763, $blacka1, $font, $content4);      // 写入积分
//            imagettftext($im, 16, 0, 490, 763, $blacka1, $font, $content5);      // 写入积分
//            imagettftext($im, 16, 0, 560, 763, $blacka, $font, $content6);      // 写入积分
//            imagettftext($im, 18, 0, 90, 793, $blacka, $font, $content7);      // 写入积分

            $upload = new Upload();
            $imgurl = $upload->save($cover);

            $image = \think\Image::open($imgurl);

            $file = ROOT_PATH.'public/nouse/'.'f'.time().rand(1,100).'.jpg';
            $image->thumb(750, 625)->save($file);

            $src_info = getimagesize($file);
            if($src_info['mime'] == 'image/png'){
                $src_im = imagecreatefrompng($file);
            }else{
                $src_im = imagecreatefromjpeg($file);
            }
            $file1 = $this->imagemerge($im,$src_im,0,0,0,0,750, 625,100);
//            $file2 = ROOT_PATH.'public/fushan.jpg';
//            $dst_im1 = imagecreatefromjpeg($file1);
//            $src_im1 = imagecreatefromjpeg($file2);
//            $src_info1 = getimagesize($file2);
//            $file3 = $this->imagemerge($dst_im1,$src_im1,180,1000,0,0,$src_info1[0], $src_info1[1],100);

            $filename = time().rand(1,999).$user['id'].'.png';
            create_qrcode('https://wx.fushan8.com/#/invitePage?id='.$user['id'],$filename,'logo.png');
//            $qrcode = Qrcode($user['token'],'https://wx.fushan8.com/#/invitePage?id='.$user['id']);
            $file4 = ROOT_PATH.'public/qrcode/'.$filename;
            $image = \think\Image::open($file4);
            $image->thumb(140, 140)->save($file4);
            $dst_im2 = imagecreatefromjpeg($file1);
            $src_im2 = imagecreatefrompng($file4);
            $src_info2 = getimagesize($file4);
            $file5 = $this->imagemerge($dst_im2,$src_im2,56,1049,0,0,$src_info2[0], $src_info2[1],100,1);
            $image = \think\Image::open(ROOT_PATH.'public/img/'.$file5);
            $image->text($daily['share_top'],$font,30,'#5B4D41',1,array(57,695))->save($file);
            $image->text($daily['share_buttom'],$font,30,'#5B4D41',1,array(57,755))->save($file);
            $image->text($daily['from'],$font,20,'#5B4D41',1,array(58,912))->save($file);

            $image->text($month,$font,17,'#5B4D41',1,array(638,879))->save($file);
            $image->text($day,$font,60,'#5B4D41',1,array(529,878))->save($file);
            $image->text($week,$font,17,'#5B4D41',1,array(638,917))->save($file);
            $image->text('————————————————————————————————————————————————',$font,10,'#EAEAEA',1,array(33,995))->save($file);

            $image->text($content1,$font,18,'#5B4D41',1,array(226,1059))->save(ROOT_PATH.'public/img/'.$file5);
            $image->text($content2,$font,18,'#5B4D41',1,array(226,1106))->save(ROOT_PATH.'public/img/'.$file5);
            $image->text($content3,$font,18,'#5B4D41',1,array(226,1152))->save(ROOT_PATH.'public/img/'.$file5);
            $image->text($content4,$font,18,'#FD892D',1,array(490,1152))->save(ROOT_PATH.'public/img/'.$file5);
//            $image->text($content5,$font,16,'#DD9D31',1,array(490,745))->save(ROOT_PATH.'public/img/'.$file5);
            $image->text($content6,$font,18,'#5B4D41',1,array(615,1152))->save(ROOT_PATH.'public/img/'.$file5);
            $image->text($content7,$font,16,'#5B4D41',1,array(67,1201))->save(ROOT_PATH.'public/img/'.$file5);
//            $image = \think\Image::open(ROOT_PATH.'public/img/'.$file5);
            unlink($file);
            unlink($file1);
//            unlink($file3);
            unlink($imgurl);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(1,'获取成功','http://'.$_SERVER['SERVER_NAME'].'/img/'.$file5);
    }

    public function imagemerge($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$alpha,$nouse = 0)
    {
        imagecopymerge($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $alpha);
        $filename = time().rand(1,9999).'.jpg';

        if($nouse == 0){
            imagejpeg($dst_im,ROOT_PATH.'public/nouse/'.$filename);     // 生成jpeg格式图片
            imagedestroy($dst_im);  // 销毁图片
            return ROOT_PATH.'public/nouse/'.$filename;
        }else{
            imagejpeg($dst_im,ROOT_PATH.'public/img/'.$filename);     // 生成jpeg格式图片
            imagedestroy($dst_im);  // 销毁图片
            return $filename;
        }
    }
    public function del()
    {
        $param = input('param.');
        $result = substr($param['file'],strripos($param['file'],"img"));
        unlink(ROOT_PATH.'public/'.$result);
    }
}